import Phaser from 'phaser';
import Character from "@/model/characters";

export class JoystickController {
    private joystickBase: Phaser.GameObjects.Graphics | null = null;
    private joystickHandle: Phaser.GameObjects.Graphics | null = null;
    private joystickVisible = false;
    private joystickRadius = 80;
    private handleRadius = 30;
    private scene: Phaser.Scene;
    private character: Character;

    constructor(scene: Phaser.Scene, character: Character) {
        this.scene = scene;
        this.character = character;
    }

    showJoystick(pointer: Phaser.Input.Pointer) {
        this.joystickVisible = true;
        
        // 创建摇杆底座
        this.joystickBase = this.scene.add.graphics();
        this.joystickBase.fillStyle(0x888888, 0.5);
        this.joystickBase.fillCircle(pointer.x, pointer.y, this.joystickRadius);
        
        // 创建摇杆手柄
        this.joystickHandle = this.scene.add.graphics();
        this.joystickHandle.fillStyle(0xAAAAAA, 0.8);
        this.joystickHandle.fillCircle(pointer.x, pointer.y, this.handleRadius);
    }

    hideJoystick() {
        this.joystickVisible = false;
        if (this.joystickBase) {
            this.joystickBase.destroy();
            this.joystickBase = null;
        }
        if (this.joystickHandle) {
            this.joystickHandle.destroy();
            this.joystickHandle = null;
        }
    }

    updateJoystick(pointer: Phaser.Input.Pointer) {
        if (!this.joystickBase || !this.joystickHandle) return;

        const baseX = this.joystickBase.x;
        const baseY = this.joystickBase.y;
        const dx = pointer.x - baseX;
        const dy = pointer.y - baseY;
        const distance = Phaser.Math.Distance.Between(baseX, baseY, pointer.x, pointer.y);
        
        let handleX = baseX;
        let handleY = baseY;

        if (distance > this.joystickRadius) {
            const angle = Math.atan2(dy, dx);
            handleX = baseX + Math.cos(angle) * this.joystickRadius;
            handleY = baseY + Math.sin(angle) * this.joystickRadius;
        } else {
            handleX = pointer.x;
            handleY = pointer.y;
        }

        this.joystickHandle.x = handleX;
        this.joystickHandle.y = handleY;
    }

    movePlayer(pointer: Phaser.Input.Pointer) {
        if (!this.joystickBase || !this.character) return;

        const baseX = this.joystickBase.x;
        const baseY = this.joystickBase.y;
        const dx = pointer.x - baseX;
        const dy = pointer.y - baseY;
        const distance = Phaser.Math.Distance.Between(baseX, baseY, pointer.x, pointer.y);
        
        let directionX = 0;
        let directionY = 0;

        if (distance > 0) {
            directionX = dx / distance;
            directionY = dy / distance;
        }

        // 计算移动速度
        const speedX = directionX * this.character.speed;
        const speedY = directionY * this.character.speed;

        // 移动 player
        this.character.x += speedX * (this.scene.game.loop.delta / 1000);
        this.character.y += speedY * (this.scene.game.loop.delta / 1000);

        // 限制 player 不超出边框
        const playerHalfWidth = this.character.width / 2;
        const playerHalfHeight = this.character.height / 2;
        this.character.x = Phaser.Math.Clamp(this.character.x, playerHalfWidth, this.scene.scale.width - playerHalfWidth);
        this.character.y = Phaser.Math.Clamp(this.character.y, playerHalfHeight, this.scene.scale.height - playerHalfHeight);
    }

    get isJoystickVisible() {
        return this.joystickVisible;
    }
}